package com.ocom.company.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityFlowApprover;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.GetFlowApprovalDetailRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.vo.activiti.SearchFlowVo;
import com.ocom.common.vo.company.GetFlowApprovalDetailVo;
import com.ocom.company.dao.IDaoFlowApprover;
import com.ocom.company.feign.ActivitiClientService;
import com.ocom.company.service.FlowApproverService;
import com.ocom.company.service.MngUserInfoService;
import com.ocom.company.service.PersonnelInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class FlowApproverServiceImpl  extends ServiceImpl<IDaoFlowApprover, EntityFlowApprover> implements FlowApproverService {

    @Autowired
    private ActivitiClientService activitiClientService;

    @Autowired
    private MngUserInfoService mngUserInfoService;

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityFlowApprover> getByFid(Long companyId, Integer fId) {
        return this.list(Wrappers.lambdaQuery(EntityFlowApprover.class)
                .eq(EntityFlowApprover::getComId,companyId)
                .eq(EntityFlowApprover::getFId,fId)
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean save(Long companyId, EntityFlowApprover entityFlowApprover) {
        return this.save(entityFlowApprover);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityFlowApprover getOne(Long companyId, EntityFlowApprover entityFlowApprover) {
        return this.getOne(Wrappers.lambdaQuery(EntityFlowApprover.class).eq(EntityFlowApprover::getFId,entityFlowApprover.getFId()).eq(EntityFlowApprover::getComId,entityFlowApprover.getComId()).eq(EntityFlowApprover::getVarTag,entityFlowApprover.getVarTag()).last("limit 1"));
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean update(Long companyId, EntityFlowApprover entityFlowApprover) {
        return this.update(entityFlowApprover,Wrappers.lambdaQuery(EntityFlowApprover.class)
                .eq(EntityFlowApprover::getId,entityFlowApprover.getId())
                .eq(EntityFlowApprover::getComId,entityFlowApprover.getComId())
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean remove(Long companyId, EntityFlowApprover entityFlowApprover) {
        if(ObjectUtil.isEmpty(entityFlowApprover.getFId()) && ObjectUtil.isEmpty(entityFlowApprover.getId())){
            log.error("删除审批人，参数FID或Id其中一个必须非空");
            throw new RuntimeException("系统错误");
        }
        return this.remove(Wrappers.lambdaQuery(EntityFlowApprover.class)
                .eq(EntityFlowApprover::getComId,entityFlowApprover.getComId())
                .eq(ObjectUtil.isNotEmpty(entityFlowApprover.getId()),EntityFlowApprover::getId,entityFlowApprover.getId())
                .eq(ObjectUtil.isNotEmpty(entityFlowApprover.getFId()),EntityFlowApprover::getFId,entityFlowApprover.getFId())
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<GetFlowApprovalDetailVo> getFlowApprovalDetail(Long companyId,GetFlowApprovalDetailRequest getFlowApprovalDetailRequest) {
        Result<List<SearchFlowVo>> searchFlowResult = this.activitiClientService.getProcessInfo(getFlowApprovalDetailRequest.getDefKey(),getFlowApprovalDetailRequest.getActivitiKey());
        List<GetFlowApprovalDetailVo> approvalDetailVoList = new ArrayList<>();
        for (SearchFlowVo searchFlowVo:searchFlowResult.getData()){
            GetFlowApprovalDetailVo getFlowApprovalDetailVo = Convert.convert(GetFlowApprovalDetailVo.class,searchFlowVo);
            getFlowApprovalDetailVo.setComment(searchFlowVo.getApplyMsg());
            if(ObjectUtil.isNotEmpty(searchFlowVo.getApprovalUser())){
                if(ObjectUtil.isNotEmpty(searchFlowVo.getUserId())){
                    searchFlowVo.getApprovalUser().add(searchFlowVo.getUserId());
                }
                for (String approvalUser: searchFlowVo.getApprovalUser()){
                    String[] strings = approvalUser.split("P");
                    Long pId = Long.valueOf(strings[strings.length - 1]);
                    if(strings.length == 1){
                        EntityMngUserInfo entityMngUserInfo =  this.mngUserInfoService.GetMngUserInfo(pId);
                        if(entityMngUserInfo!=null){
                            getFlowApprovalDetailVo.getApproverIds().add(entityMngUserInfo.getId());
                            getFlowApprovalDetailVo.getApproverNames().add(entityMngUserInfo.getUserName());
                        }
                    }else{
                        Long comId = Long.valueOf(strings[strings.length - 2]);
                        EntityPersonnelInfo entityPersonnelInfo = this.personnelInfoService.getPerInfo(comId,pId);
                        if(entityPersonnelInfo!=null){
                            getFlowApprovalDetailVo.getApproverIds().add(entityPersonnelInfo.getPId());
                            getFlowApprovalDetailVo.getApproverNames().add(entityPersonnelInfo.getPName());
                        }
                    }
                }
            }else{
                String[] strings = searchFlowVo.getUserId().split("P");
                Long pId = Long.valueOf(strings[strings.length - 1]);
                EntityPersonnelInfo entityPersonnelInfo = this.personnelInfoService.getPerInfo(companyId,pId);
                if(entityPersonnelInfo!=null){
                    getFlowApprovalDetailVo.getApproverIds().add(entityPersonnelInfo.getPId());
                    getFlowApprovalDetailVo.getApproverNames().add(entityPersonnelInfo.getPName());
                }
            }
            approvalDetailVoList.add(getFlowApprovalDetailVo);
        }
        return approvalDetailVoList;
    }
}
